<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form :model="regForm" :rules="regFormRules" ref="regFormRef">
        <el-form-item prop="username">
          <el-input
            v-model="regForm.username"
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="regForm.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="regForm.repassword"
            prefix-icon="el-icon-lock"
            placeholder="请再次输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="doRegister">注册</el-button>
          <el-link :underline="false" href="#/login">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data () {
    return {
      // 1.表单数据
      regForm: {
        username: '',
        password: '',
        repassword: ''
      },
      // 2.表单校验
      regFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: '用户名由1到10位的字母和数字组成',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码由6到15位非空字符组成',
            trigger: 'blur'
          }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码由6到15位非空字符组成',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              if (value === this.regForm.password) {
                callback()
              } else {
                callback(new Error('两次输入密码不一致!'))
              }
            },
            message: '两次输入密码不一致!',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  // methods:事件处理函数
  methods: {
    doRegister() {
      // 表单验证是否通过
      this.$refs.regFormRef.validate(async valid => {
        console.log(valid)
        // 校验通过
        if (valid) {
          // 发送ajax
          const res = await this.$axios.post('/api/reg', this.regForm)
          console.log(res)
          // 提示消息
          this.$message.success(res.data.message)
          // 注册成功跳转登录页
          if (res.data.code === 0) {
            this.$router.push('/login')
          }
        }
      })
    }
  },
}
</script>

<style lang="less" scoped>
.reg-container {
  background: url('@/assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title-box {
      height: 60px;
      background: url('@/assets/images/login_title.png') center no-repeat;
    }
  }

  .el-form {
    padding: 0 25px;
  }

  .btn-reg {
    width: 100%;
  }
}
</style>
